package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.entity.Station;
import com.ruoyi.web.service.IStationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-25
 */
@RestController
@RequestMapping("/admin/station")
public class StationController extends BaseController {

    private final IStationService stationService;

    public StationController(IStationService stationServiceImpl) {
        this.stationService = stationServiceImpl;
    }


    /**
     *
     * @return 获取电站信息
     */
    @GetMapping("/getDatas")
    public AjaxResult getDatas() {
        List<Station> list = stationService.list();
        List<Map<String, Object>> datas = new ArrayList<>();
        for (Station station : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("stationId", station.getId().toString());
            map.put("stationName", station.getStationName());
            datas.add(map);
        }
        return success(datas);
    }

    /**
     * 查询充电站列表
     */
    @PreAuthorize("@ss.hasPermi('admin:station:list')")
    @GetMapping("/list")
    public TableDataInfo list(Station station) {
        startPage();
        List<Station> list = stationService.selectStationList(station);
        return getDataTable(list);
    }

    /**
     * 导出充电站列表
     */
    @PreAuthorize("@ss.hasPermi('admin:station:export')")
    @Log(title = "充电站", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Station station) {
        List<Station> list = stationService.selectStationList(station);
        ExcelUtil<Station> util = new ExcelUtil<Station>(Station.class);
        util.exportExcel(response, list, "充电站数据");
    }

    /**
     * 获取充电站详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:station:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stationService.selectStationById(id));
    }

    /**
     * 新增充电站
     */
    @PreAuthorize("@ss.hasPermi('admin:station:add')")
    @Log(title = "充电站", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Station station) {
        Station dbStation = stationService.getOne(Wrappers.lambdaQuery(Station.class)
                .eq(Station::getStationName, station.getStationName()));
        if (Objects.nonNull(dbStation)) {
            throw new RuntimeException("电站名字重复，以防混淆，请重新输入");
        }
        return toAjax(stationService.insertStation(station));
    }

    /**
     * 修改充电站
     */
    @PreAuthorize("@ss.hasPermi('admin:station:edit')")
    @Log(title = "充电站", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Station station) {
        Station dbStation = stationService.getOne(Wrappers.lambdaQuery(Station.class)
                .eq(Station::getStationName, station.getStationName())
                .ne(Station::getId,station.getId()));
        if (Objects.nonNull(dbStation)) {
            throw new RuntimeException("电站名字重复，以防混淆，请重新输入");
        }
        return toAjax(stationService.updateById(station));
    }

    /**
     * 删除充电站
     */
    @PreAuthorize("@ss.hasPermi('admin:station:remove')")
    @Log(title = "充电站", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stationService.deleteStationByIds(ids));
    }
}
